/*
 * 纱嘴替换
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "yarn_change.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "com.h"
#include "keypane.h"
#include "info.h"


#define YARN_CHANGE_CAPTION_WIDTH   (80)
#define YARN_CHANGE_CAPTION_HEIGHT  (24)
#define YARN_CHANGE_CAPTION_INTER   (3)
#define YARN_CHANGE_COL_WIDTH       (90) //total width / 3
#define YARN_CHANGE_CAPTION_LEFT    (CX_EDGE_LEFT * 2)
#define YARN_CHANGE_CAPTION_TOP     (CY_TITLE4_HEIGHT + CX_EDGE_LEFT - 5)
#define YARN_CHANGE_NAME_WIDTH      (60)


/*
 *
 */
static void CreateYarnChangeInterface(HWND hWnd)
{
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);

    CreateWindow (MH_LABEL_NAME,
        MW_NUM_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "1.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER),
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "2.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 2,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "3.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 3,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "4.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 4,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "5.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 5,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "6.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 6,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "7.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 7,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "8.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 8,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "9.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 9,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        "10.",
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * 10,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        MW_SRC_YARN_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_COL_WIDTH + YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    //
    CreateWindow (MH_LABEL_NAME,
        MW_DES_YARN_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        YARN_CHANGE_COL_WIDTH * 2 + YARN_CHANGE_CAPTION_LEFT,
        YARN_CHANGE_CAPTION_TOP,
        YARN_CHANGE_CAPTION_WIDTH,
        YARN_CHANGE_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    int i, j;
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 10; ++j)
        {
            CreateWindow (MH_EDIT_NAME,
                "0",
                WS_CHILD | BS_CENTER | WS_VISIBLE,
                IDC_EDIT1 + i * 10 + j,
                YARN_CHANGE_COL_WIDTH * (i + 1) + YARN_CHANGE_CAPTION_LEFT,
                YARN_CHANGE_CAPTION_TOP + (YARN_CHANGE_CAPTION_HEIGHT + YARN_CHANGE_CAPTION_INTER) * (j + 1),
                YARN_CHANGE_NAME_WIDTH,
                YARN_CHANGE_CAPTION_HEIGHT,
                hWnd, 0);
        }
    }
}
/*
 *
 */
static int ChangeYarn(HWND hWnd)
{
    int ret = FALSE;
    int i = 0;
    for(i = 0; i < 10; ++i)
    {
        const char *sc = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + i));
        uchar src_yarn = atoi(sc);
        sc = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 10 + i));
        uchar des_yarn = atoi(sc);
        if(src_yarn == des_yarn)
        {
            continue;
        }
        if(src_yarn == 9 || src_yarn == 10)
        {
            src_yarn += 2;
        }
        if(des_yarn == 9 || des_yarn == 10)
        {
            des_yarn += 2;
        }
        if(src_yarn < 1 || src_yarn > 18)
        {
            continue;
        }
        if(des_yarn < 1 || des_yarn > 18)
        {
            continue;
        }
        ReplaceYarn(g_knit_data, src_yarn, des_yarn);
        ret = TRUE;
    }
    return ret;
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, ushort nID)
{
    SetWindowAdditionalData(hWnd, TRUE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    if(nID == ID_ESC)
    {
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 20)
    {
        HWND hTmp = GetDlgItem(hWnd, nID);
        RECT rect;
        GetClientRect(hTmp, &rect);
        ClientToScreen(hTmp, &rect.left, &rect.top);
        ClientToScreen(hTmp, &rect.right, &rect.bottom);
        CreateKeyboardWindow(hWnd, rect, 0, 18, atoi(GetWindowCaption(hTmp)));
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        if(GetWindowAdditionalData(hWnd))
        {
            if(ChangeYarn(hWnd))
            {
                char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR)
                        + sizeof(CON_FILE_SUFFIX)] = { 0 };
                strcat(sc, KNIT_FILE_DIR);
                strcat(sc, GetKnitFileName(g_knit_data));
                strcat(sc, CON_FILE_SUFFIX);
                int ret = WriteConFile(sc, g_knit_data);
                if(ret)
                {
                    printf("failed to write con file %s: %d\n", sc, ret);
                    AfxInfoBox(hWnd, NULL, GetString(590));
                }
                else
                {
                    MH_FILE_SYNC();
                }
                SendControlData(g_knit_data);
            }
        }
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 + 10 && nID < IDC_EDIT1 + 20)
        {
            nID -= 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 10)
        {
            nID += 10;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        int n = nID - IDC_EDIT1;
        if((n > 0 && n < 10) || (n > 10 && n < 20))
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        int n = nID - IDC_EDIT1;
        if((n >= 0 && n < 9) || (n >= 10 && n < 19))
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int YarnChangeWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateYarnChangeInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_SETDATA:
        {
            OnSetKnitValue(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateYarnChangeWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_YARN_EXCHANGE_SET_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = YarnChangeWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
